package com.maimang.platform.controller.monitor;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysApiLog;
import com.maimang.platform.common.service.SysApiLogService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @classname SysApiLogController
 * @description <br>远程API接口请求日志记录信息操作处理</br>
 * @author  Rocky
 * @date    2019-09-04 16:37:40
 * @email   jintaozhao@qq.com
 * @version 1.0.0
 */
@Controller
@RequestMapping("/monitor/apilog")
@Api(value = "远程API接口请求日志记录", description = "远程API接口请求日志记录 信息操作处理接口")
public class SysApiLogController extends BaseController {
    private String prefix = "monitor/apilog";

	@Autowired
	private SysApiLogService sysApiLogService;

	@RequiresPermissions("monitor:apilog:view")
	@GetMapping()
	public String sysApiLog() {
	    return prefix + "/apilog";
	}

	@RequiresPermissions("monitor:apilog:detail")
	@GetMapping("/detail/{logId}")
	public String detail(@PathVariable("logId") Integer logId, ModelMap modelMap) {
		modelMap.put("apiLog", sysApiLogService.selectSysApiLogById(logId));
		return prefix + "/detail";
	}

	/**
	 * 查询远程API接口请求日志记录列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询远程API接口请求日志记录列表信息")
	@RequiresPermissions("monitor:apilog:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysApiLog sysApiLog) {
		startPage();
        List<SysApiLog> list = sysApiLogService.selectSysApiLogList(sysApiLog);
		return getDataTable(list);
	}


	/**
	 * 导出远程API接口请求日志记录列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出远程API接口请求日志信息到Excel文件")
	@RequiresPermissions("monitor:apilog:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(SysApiLog sysApiLog) {
    	List<SysApiLog> list = sysApiLogService.selectSysApiLogList(sysApiLog);
        ExcelUtil<SysApiLog> util = new ExcelUtil<SysApiLog>(SysApiLog.class);
        return util.exportExcel(list, "sysApiLog");
    }

	/**
	 * 新增远程API接口请求日志记录
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增保存远程API接口请求日志记录
	 */
	@ApiOperation(value = "新增保存", notes = "参数：SysApiLog")
	@RequiresPermissions("monitor:apilog:add")
	@Log(title = "远程API接口请求日志记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysApiLog sysApiLog) {
		return toResult(sysApiLogService.insertSysApiLog(sysApiLog));
	}

	/**
	 * 修改远程API接口请求日志记录
	 */
	@GetMapping("/edit/{logId}")
	public String edit(@PathVariable("logId") Integer logId, ModelMap modelMap) {
		SysApiLog sysApiLog = sysApiLogService.selectSysApiLogById(logId);
		modelMap.put("sysApiLog", sysApiLog);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存远程API接口请求日志记录
	 */
	@ApiOperation(value = "修改保存", notes = "参数：SysApiLog")
	@RequiresPermissions("monitor:apilog:edit")
	@Log(title = "远程API接口请求日志记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysApiLog sysApiLog) {
		return toResult(sysApiLogService.updateSysApiLog(sysApiLog));
	}

	/**
	 * 删除远程API接口请求日志记录
	 */
	@ApiOperation(value = "删除对象", notes = "参数：ids，示例： id1,id2,id3，接口会转转为String[]类型")
	@RequiresPermissions("monitor:apilog:remove")
	@Log(title = "远程API接口请求日志记录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(sysApiLogService.deleteSysApiLogByIds(ids));
	}

	@Log(title = "接口日志", businessType = BusinessType.CLEAN)
	@RequiresPermissions("monitor:apilog:remove")
	@PostMapping("/clean")
	@ResponseBody
	public Result clean() {
		sysApiLogService.cleanApiLog();
		return success();
	}
}